package com.liuzhilin.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.liuzhilin.domain.User;
import com.liuzhilin.service.ManagerService;
import com.liuzhilin.util.Response;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/ManagerGetUser")
public class ManagerGetUser extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 设置CORS响应头
        Response.setCorsHeaders(response);
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json");
        PrintWriter out = response.getWriter();

        // 从请求中获取管理员用户名
        String adminUsername = request.getParameter("username");
        adminUsername = adminUsername.trim();
        // 获取用户列表
        List<User> users = ManagerService.getUsers(adminUsername);

        // 将用户列表转换为JSON格式
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(users);

        // 返回用户列表
        out.print(json);
    }
    @Override
    public void doOptions(HttpServletRequest request, HttpServletResponse response){
        Response.setCorsHeaders(response);
    }
}
